if(!window.xflib || !window.xflib.client) window.xflib = { client: {} }

xflib.client.ProjectModal = class ProjectModal {

    static modal() { return document.getElementById('project-modal'); }
    static title() { return xflib.client.ProjectModal.modal().querySelector('#modal-title'); }
    static projectCodeInput() { return xflib.client.ProjectModal.modal().querySelector('#project-code'); }
    static projectNameInput() { return xflib.client.ProjectModal.modal().querySelector('#project-name'); }
    static projectUrlInput() { return xflib.client.ProjectModal.modal().querySelector('#project-url'); }
    static projectCategoryInput() { return xflib.client.ProjectModal.modal().querySelector('#project-category'); }
    static projectSerialInput() { return xflib.client.ProjectModal.modal().querySelector('#project-serial'); }
    static projectPermissionsInput() { return xflib.client.ProjectModal.modal().querySelectorAll('input[name="project-permission"]'); }
    static productCodeInput() { return xflib.client.ProjectModal.modal().querySelector('#product-code'); }

    static projectCategorySelect() { return xflib.client.ProjectModal.modal().querySelector('#project-category'); }
    static productCodeSelect() { return xflib.client.ProjectModal.modal().querySelector('#product-code'); }

    constructor() {
        this.mode = 'info';
        this.modal = xflib.client.ProjectModal.modal();
        this.initializeEventListeners();
    }

    initializeEventListeners() {
        xflib.client.ProjectModal.modal().querySelectorAll('.modal-close').forEach(btn => {
            btn.addEventListener('click', e => {
                if (this.modal) this.modal.style.display = 'none';
            });
        });

        const cancelButton = this.modal.querySelector('#cancel-button');
        cancelButton.addEventListener('click', () => {
            this.closeModal();
        });

        const saveButton = this.modal.querySelector('#save-button');
        saveButton.addEventListener('click', () => {
            this.saveProject();
        });
        const projectUrl =this.modal.querySelector("#project-url");
        
        projectUrl.addEventListener('change', (e) => {
            const projectInfo =this.parseUrl(projectUrl.value);

            const productCode = xflib.client.ProjectModal.productCodeInput().value;
            if(productCode.trim().length === 0){
                xflib.client.ProjectModal.productCodeInput().value = projectInfo.product;
            }
            const categoryCode = xflib.client.ProjectModal.projectCategoryInput().value;
            if(categoryCode.trim().length === 0){
                xflib.client.ProjectModal.projectCategoryInput().value = projectInfo.category;
            }
            const moduleCode = xflib.client.ProjectModal.projectNameInput().value;
            if(moduleCode.trim().length === 0){
                xflib.client.ProjectModal.projectNameInput().value = projectInfo.module;
            }
        });
            
    }

    closeModal() {
        this.modal.style.display = 'none';
    }

    openModal(mode, saveCallback, projectCode = null, projectName = null, projectUrl = null, projectCategory = null, projectSerial = null, permissions = [], productCode = '', cancelCallback = null) {
        this.mode = mode;

        if (!saveCallback) throw new Error('saveCallback is required');
        if (projectCode === 'all') throw new Error('请选择需要编辑的项目');
        this.saveCallback = saveCallback;
        this.cancelCallback = cancelCallback || (() => {});

        if (mode === 'add') {
            xflib.client.ProjectModal.title().textContent = '项目 - 新增';
            // 默认勾选权限
            xflib.client.ProjectModal.projectPermissionsInput().forEach(cb => {
                cb.checked = cb.defaultChecked;
            });
            // 生成项目代码
            projectCode = projectCode || '';
            if(projectCode.trim().length === 0){
                projectCode = projectCode || xflib.client.Application.UUID();
            }
            // 设置序号
            projectSerial = projectSerial || '';
            if(projectSerial.trim().length === 0){
                projectSerial = 99;
            }
        } else if (mode === 'edit') {
            xflib.client.ProjectModal.title().textContent = '项目 - 编辑';
            // 设置权限
            xflib.client.ProjectModal.projectPermissionsInput().forEach(cb => {
                cb.checked = permissions.includes(cb.value);
            });
        } else {
            xflib.client.ProjectModal.title().textContent = '项目 - 查看';
        }

        xflib.client.ProjectModal.projectCodeInput().value = projectCode || '';
        xflib.client.ProjectModal.projectNameInput().value = projectName || '';
        xflib.client.ProjectModal.projectUrlInput().value = projectUrl || '';
        xflib.client.ProjectModal.projectCategoryInput().value = projectCategory || 'all';
        xflib.client.ProjectModal.projectSerialInput().value = projectSerial || '';
        // xflib.client.ProjectModal.projectPermissionsInput().value = permissions.join(', ') || '';
        xflib.client.ProjectModal.productCodeInput().value = productCode || 'all';

        this.modal.style.display = 'block';
    }

    saveProject() {
        const projectCode = xflib.client.ProjectModal.projectCodeInput().value.trim();
        const projectName = xflib.client.ProjectModal.projectNameInput().value.trim();
        const projectUrl = xflib.client.ProjectModal.projectUrlInput().value.trim();
        const projectCategory = xflib.client.ProjectModal.projectCategoryInput().value.trim();
        const projectSerial = xflib.client.ProjectModal.projectSerialInput().value.trim();
        // const permissions = xflib.client.ProjectModal.projectPermissionsInput().value.split(',').map(p => p.trim());
        const permissions = Array.from(xflib.client.ProjectModal.projectPermissionsInput()).filter(cb => cb.checked).map(cb => cb.value).join(',');
        const productCode = xflib.client.ProjectModal.productCodeInput().value.trim();

        if (!projectCode || !projectName) {
            alert('项目代码和名称不能为空');
            return;
        }

        this.saveCallback(projectCode, projectName, projectUrl, projectCategory, projectSerial, permissions, productCode, this.mode).then(result => this.closeModal());
    }

    parseUrl(url) {
        const urlObj = new URL(url);
        const pathParts = urlObj.pathname.replace(/^\//, '').split('/');
        return {
            product: pathParts[0]||'',
            module: pathParts[1]||'',
            category: pathParts[pathParts.length-1]||'',
            partialUrl: urlObj.pathname.replace(/^\//, '')||''
        };
    }
    
} 